﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace Xray.Common
{
    /// <summary>
    /// a component is the implemented version for a service not the interface
    /// for example IMessagingService -> EmailMessagingService. the component is the EmailMessagingService
    /// </summary>
    [DataContract(IsReference = true)]
    public class Component
    {
        public Component()
        {
            Dependents = new List<Component>();
        }

        [DataMember]
        public string Namespace { get; set; }
        
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// what the component is dependent upon
        /// </summary>
        [DataMember]
        public IList<Component> Dependents { get; set; }

        /// <summary>
        /// use this method to add dependents, as it will make sure there
        /// are no duplications in the dependents list
        /// </summary>
        /// <param name="depentent"></param>
        public  void AddDependent(Component depentent)
        {
            if (Dependents.Contains(depentent))
            {
                return;
            }

            Dependents.Add(depentent);
        }

        public string FullName()
        {
            return Namespace + "." + Name;
        }

        public override int GetHashCode()
        {
            return FullName().GetHashCode();
        }

        public override string ToString()
        {
            return FullName();
        }

        public override bool Equals(object obj)
        {
            Component component = obj as Component;
            if (component == null)
            {
                return false;
            }

            return FullName() == component.FullName();
        }
    }
}